<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('修改【请填写功能名称】')"/>
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <th:block th:include="include :: summernote-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-analyse-edit">
        <div class="panel panel-default" th:each="analyze,index_:${analyses}">
            <input name="id" type="hidden" th:value="${analyze.id}"/>
            <input name="cardId" type="hidden" th:value="${analyze.cardId}"/>
            <input name="cardInfoId" type="hidden" th:value="${analyze.cardInfoId}"/>
            <div class="panel-heading">
                <h3 class="panel-title">部件分析录入[[${index_.index+1}]]</h3>
            </div>
            <div class="panel-body">
                <label class="col-sm-1 control-label">文件：</label>
                <div class="col-sm-3"
                     th:include="system/common/file_upload :: multiple_img(_file_id='multipleFile_analyze'+${index_.index},_file_type='img',_file_list=${@file.getFiles(analyze.cardId,analyze.id,'supplier_logo'+analyze.cardInfoId)})"/>
                <input type="hidden" name="content" th:name1="content+${index_.index}" th:value="${analyze.content}"/>
                <div class="summernote" th:id="content+${index_.index}"></div>
                <button th:onclick="'delRow('+${analyze.id}+')'">删除</button>
            </div>
        </div>
    </form>
    <button onclick="submitHandler()">上传</button>
    <button onclick="addRow()">添加一行</button>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: bootstrap-fileinput-js"/>
<th:block th:include="include :: summernote-js"/>
<script th:inline="javascript">
    var prefix = ctx + "system/analyse";
    $("#form-analyse-edit").validate({
        focusCleanup: true
    });
    function flushPage(){
        window.location=prefix+'/[[${carId}]]/[[${carInfoId}]]';
    }

    function addRow(){
        // window.location=prefix+'/add/[[${carId}]]/[[${carInfoId}]]';
        $.operate.save(prefix + "/add", 'cardId=[[${carId}]]&cardInfoId=[[${carInfoId}]]',flushPage());
    }
    function delRow(id){
        window.location=prefix+'/remove/id='+id;
        refresh();
    }

    function submitHandler() {
        if ($.validate.form()) {
            let data = $('#form-analyse-edit').serialize();
            console.log(data)
            console.log(JSON.stringify(data))
            // return ;
            $.operate.save(prefix + "/edit", data);
        }
    }

    $(function () {
        $('.summernote').each(function (i) {
            $('#' + this.id).summernote({
                lang: 'zh-CN',
                dialogsInBody: true,
                callbacks: {
                    onChange: function (contents, $edittable) {
                        $("input[name='" + this.id + "']").val(contents);
                    },
                    onImageUpload: function (files) {
                        var obj = this;
                        var data = new FormData();
                        data.append("file", files[0]);
                        $.ajax({
                            type: "post",
                            url: ctx + "common/upload",
                            data: data,
                            cache: false,
                            contentType: false,
                            processData: false,
                            dataType: 'json',
                            success: function (result) {
                                if (result.code == web_status.SUCCESS) {
                                    $('#' + obj.id).summernote('insertImage', result.url);
                                } else {
                                    $.modal.alertError(result.msg);
                                }
                            },
                            error: function (error) {
                                $.modal.alertWarning("图片上传失败。");
                            }
                        });
                    }
                }
            });
            var content = $("input[name1='" + this.id + "']").val();
            $('#' + this.id).summernote('code', content);
        });

        /*<![CDATA[*/
        /*[#  th:each="analyze,index_:${analyses}"]*/
        multipleFile_analyze[[${index_.index}]]_manager.init({
            busId: [[${analyze.cardId}]],
            busType: [[${analyze.id}]],
            fileType: 'img',
            groupType: '[[${analyze.cardInfoId}]]'
        });
        /*[/]*/
        /*]]>*/
    });
</script>
</body>
</html>